const enterpriseVue = new Vue({
  el: '#app',
  data() {
    //校验身份证号
    // var checkIDCard = (rule, value, callback) => {
    //   if (!value) {
    //     return callback(new Error('身份证号不能为空'));
    //   }
    //   setTimeout(() => {
    //     if (!ID_Validity(value)) {
    //       callback(new Error('请输入正确的身份证号'));
    //     }
    //   }, 100);
    // }
    return {
      rules: {
        Enterprise_Info_SerialNumber_A: [{ required: true, message: '请输入公司', trigger: 'blur' }],
        Total_Amount: [{ required: true, message: '请输入总金额', trigger: 'blur' }],
        Mobile: [{ required: true, message: '请选择支付方式', trigger: 'blur' }],
      },
      dialogVisible: false,
      flag: true,
      enterprise: [],
      form: {
        Coupon_Total_SerialNumber: '',//主键
        Total_Amount: 0,//总额
        Received_Amount: 0, //已收金额
        Uncollected_Amount: 0, //未收金额
        Coupon_Total_Quantity: 0,//总数量
        Consumption_Amount: 0,//消耗
        Remain_Amount: 0,//剩余
        Mobile: '', //支付方式
        Enterprise_Info_SerialNumber_A: '', //用卷企业
        Enterprise_Info_SerialNumber_B: $.cookie("enterprise"),
        Children: {
          coupon_info: [{
            Coupon_SerialNumber: '',//主键
            Coupon_Total_SerialNumber: '', //外键
            Coupon_Price: 0,//优惠卷面额
            Coupon_Type_SerialNumber: '',//优惠卷类型
            Coupon_Quantity: 0,//优惠卷总量
            Coupon_Remain: 0,//优惠卷剩余数量
            Coupon_Send_Out: 0,//优惠卷发布数量
            Coupon_Total_Price: 0, //优惠卷总面额
            Take_Effect: 0, //启用状态
            Children: {
              coupon_detail_info: []
            }
          }],
        }
      },
      loading: false
    }
  },
  mounted() {
    this.getEnterprise()
  },
  computed: {
    //该计算属性返回要监视的对象属性值
    infoa() {
      return this.form.Total_Amount;
    }
  },
  watch: {
    //开始监听返回该对象属性值计算属性
    infoa: function (newVal) {
      if (newVal == '' || newVal == 0) {
        this.flag = true
      } else {
        this.flag = false
      }
    }
  },
  methods: {
    getEnterprise() {
      let _this = this;
      $.ajax({
        url: DomainName + "/GoodsFileInquiry",
        type: 'POST',
        data: { Data: "enterprise_info" },
        success: function (res) {
          _this.enterprise = res
          console.log(_this.enterprise)
        },
        error: function () {
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    hanldeChange(index) {
      let _this = this

      this.form.Children.coupon_info[index].Coupon_Total_Price = this.form.Children.coupon_info[index].Coupon_Price * this.form.Children.coupon_info[index].Coupon_Quantity
      let num = 0
      let number = 0
      this.form.Children.coupon_info.forEach(item => {
        if (item.Coupon_Price || item.Coupon_Quantity) {

          num = num + parseInt(item.Coupon_Total_Price)
          number = number + parseInt(item.Coupon_Quantity)
          _this.form.Consumption_Amount = num
          _this.form.Coupon_Total_Quantity = number
          _this.form.Children.coupon_info[index].Coupon_Remain = number
          _this.form.Remain_Amount = _this.form.Total_Amount - _this.form.Consumption_Amount
        }
        // _this.form.Consumption_Amount = 0
        // _this.form.Coupon_Total_Quantity = 0
        // _this.form.Children.coupon_info.Coupon_Remain = 0
        // _this.form.Remain_Amount = 0
      })
      if (_this.form.Remain_Amount < 0) {
        return this.$message('已超出总金额');
      }
      // if (_this.form.Consumption_Amount == NaN) _this.form.Consumption_Amount = 0
      // if (_this.form.Coupon_Total_Quantity == NaN) _this.form.Coupon_Total_Quantity = 0
      // if (_this.form.Children.coupon_info.Coupon_Remain == NaN) _this.form.Children.coupon_info.Coupon_Remain = 0
      // if (_this.form.Remain_Amount == NaN) _this.form.Remain_Amount = 0
    },
    submitForm(formName) {
      if (this.form.Remain_Amount < 0) return this.$message('已超出总金额');
      let _this = this
      this.form.Children.coupon_info.forEach(item => {
        // console.log(item.Children.coupon_detail_info)
        for (let i = 0; i < item.Coupon_Quantity; i++) {
          console.log(item.Take_Effect)
          item.Children.coupon_detail_info.push({
            Coupon_Detail_SerialNumber: '', //主键
            Coupon_SerialNumber: '', //外键
            Coupon_Price: item.Coupon_Price, //优惠卷面额
            Coupon_Type_SerialNumber: item.Coupon_Type_SerialNumber, //优惠券类型
            Take_Effect: item.Take_Effect //启用状态
          })
        }
      })
      console.log(formName)
      _this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          console.log(_this.form);
          $.ajax({
            url: DomainName + "/getAddAction",
            type: 'POST',
            data: {
              Data: JSON.stringify({
                coupon_total: [_this.form]
              }),
              token: $.cookie("token")
            },
            success: function (res) {
              console.log(res)

              if (res.Status === 'success') {
                let obj = {
                  Coupon_Journal_SerialNumber: '',  //主键
                  Coupon_Total_SerialNumber: res.Data,  //流水号
                  Total_Amount: _this.form.Total_Amount,    //总金额
                  Coupon_Total_Quantity: _this.form.Coupon_Total_Quantity,  //制卷总数量
                  Consumption_Amount: _this.form.Consumption_Amount,  //制券金额
                  Remain_Amount: _this.form.Remain_Amount,  //剩余金额
                }
                console.log(obj)
                $.ajax({
                  url: DomainName + "/getAddAction",
                  type: 'POST',
                  data: {
                    Data: JSON.stringify({
                      coupon_journal: [obj]
                    }),
                    token: $.cookie("token")
                  },
                  success: function (res) {
                    _this.$message.success("提交成功")
                    window.location.href = './Coupon.html'
                    if (res.Status === 'success') {
                    } else {
                      _this.$message.error("提交失败，请稍后再试")
                    }
                  },
                  error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                  },
                  complete: function (XMLHttpRequest) {
                    _this.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                      window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                  }
                })
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }

      });
    },
    // 新增购物卷
    add_member_account_info() {
      this.form.Children.coupon_info.push({
        Coupon_SerialNumber: '',//主键
        Coupon_Journal_SerialNumber: '', //外键
        Coupon_Price: 0,//优惠卷面额
        Coupon_Type_SerialNumber: '',//优惠卷类型
        Coupon_Quantity: 0,//优惠卷总量
        Coupon_Remain: 0,//优惠卷剩余数量
        Coupon_Send_Out: 0,//优惠卷发布数量
        Coupon_Total_Price: 0, //优惠卷总面额
        Take_Effect: 0, //启用状态
        Children: {
          coupon_detail_info: []
        }
      })
    },
    // 删除购物卷
    remove_member_account_info() {
      if (this.form.Children.coupon_info.length > 1) {
        this.form.Children.coupon_info.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
})